/**
 * 回访内容
 *
 * @author linfeng
 * @since 2019/10/23
 */
layui.extend({
    zmnExportUtil: '{/}/static/js/zmn.excel.util'
}).define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate', 'zmnExportUtil'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    let zmnExportUtil =layui.zmnExportUtil;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-110',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        }
    });
    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnOmsPage.laypageOptions(json)));

    var ids = new Array();

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        let items = obj.config.data;
        ids = zmnOmsPage.layIdsChecked(items, "answerId");

        //导出excel
        if(obj.event=='CUSTOM_EXPORT'){
            zmnExportUtil.doExportExcel('zmn-table','回访答案');
        }

        if (obj.event != "start" && obj.event != "stop") {
            return;
        }
        if (ids.length === 0) {
            return layer.alert("请选择至少一选择一条数据！");
        }
        zmnOmsPage.layStartStop(obj, "/conf/reply/answer/updateStatus.action", ids.join(","));
    });

    // 修改排序
    table.on("edit(zmn-table)", function (obj) {
        if(obj.field=='sort'){
            if (!/^[1-9]\d*$/.test(obj.value)) {
                layer.alert("排序值只能是正整数");
                return false;
            }
            if(obj.value > 100000){
                layer.alert("排序最大100000");
                return false;
            }
            zmnOmsPage.laySort(obj, "/conf/reply/answer/updateSort.action", obj.data.answerId);
        }else if(obj.field=='firstLetter'){
            let firstLetter = obj.value;
            let flag = /[aA-zZ]/.test(firstLetter);
            if (!flag){
                layer.alert("请输入小/大写英文字母");
                return false;
            }
            if (firstLetter.length > 1){
                layer.alert("首字母字段长度不能超过1位");
                return false;
            }
            zmnOmsPage.ajaxSimple("/conf/reply/answer/updateFirstLetter.action", {id: obj.data.answerId, firstLetter: firstLetter.toUpperCase()}, 1000);
        }

    });

    //对外暴露的接口
    exports('oms_conf_reply_answer_list', {});
});

/**
 * 描述: 新增/修改回访答案
 **/
function addEditReturnVisitAnswer(id){
    if (id == ""){
        zmnLayUtil.openIframeCustom(layer, OMS_BASE_URL + "/conf/reply/answer/detail.action", {title: "新增", shade: 0.8, area:["700px", "260px"]});
    } else {
        zmnLayUtil.openIframeCustom(layer, OMS_BASE_URL + "/conf/reply/answer/detail.action?id="+id, {title: "修改", shade: 0.8, area:["700px", "260px"]});
    }
}
